package com.fyyg.common.ssc;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.common.json.ParseException;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fyyg.common.sms.yimei.HttpUtil;
import com.fyyg.common.util.date.DateUtil;
import com.fyyg.common.util.date.YXDate;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.net.ntp.TimeStamp;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.http.converter.json.GsonFactoryBean;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author hang.ouyang
 * @date 2016-04-21 11:06
 */
public class SSCUtil {

    private final static String reqPath = "http://c.apiplus.net/newly.do?token=%s&code=cqssc&format=json&rows=%d";

    public static List<SSCObject> getRecentCodeList(String env,Integer num,Integer errorTime) {
        num = num == null ? 1 : num;
        String sscToken = env != null && env.toLowerCase().trim().equals("product") ? "wx73212669864d0b" : "e7ecb289e79b62ff" ;
        String path = String.format(reqPath,sscToken,num);
        String result = HttpUtil.sendGet(path);
        List<SSCObject> list = new ArrayList<>();
        try{
            System.out.println("env\t"+env+"\ttoken \t"+sscToken+"\tssc\t"+result);
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(result).getString("data"));
            for (int i = 0; i < jsonArray.size() ; i++) {
                SSCObject ssc =  JSON.parse(jsonArray.getString(i),SSCObject.class);
                list.add(ssc);
            }
        } catch (Exception e) {
            System.out.println("ssc result\t"+result+"获取时时彩异常\t"+e.getMessage());
            errorTime ++;
            if(errorTime >= 3){
                list.add(SSCObject.emptyObject());
                return list;
            }
            getRecentCodeList(env,num,errorTime);
        }
        return list;
    }


    /**
     * 查询最近一期（下一期）时时彩开奖号码
     * @return
     */
    public static SSCObject getRecentCode(String env){
        List<SSCObject> list = getRecentCodeList(env,1,0);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }


    public static  SSCObject getTimeAfterCode(Date date,String env){
            SSCObject object = null;
            try {
                while (true){
                     Thread.sleep(5000L);
                    object = getRecentCode(env);
                    if(object == null)
                        continue;
                    if (Long.parseLong(object.getOpentimestamp()) > date.getTime()/1000L){
                        return object;
                    }
                    System.out.println("retry to get new ssc result....");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        return object;
    }



    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < "12301".length(); i++) {
            sb.append("12301".charAt(i));
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        System.out.println(sb.toString());
    }



}
